import { _decorator, Button, Label, Node, ProgressBar, RichText, UITransform } from 'cc';
import { BasePopUp } from '../../../A-FRAME/component/ui.pop-up';
import { UserData } from '../../../Data/UserData';
import { Guide } from '../../../game/Guide';
import { AudioMgr } from '../../../Mgr/AudioMgr';
import { UIMgr } from '../../../Mgr/UIMgr';
import LocalizedLabel from '../../../Tools/i18n/LocalizedLabel';
import { Url } from '../../../Tools/Url';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('WithdrawPage')
export class WithdrawPage extends BasePopUp<typeof Popup, { type: string, value: number }> {
    protected default_return: { type: string; value: number; };

    private ui_container: UITransform;
    @property(Node)
    selecthitBtns: Node[] = [];
    @property(Label)
    wdBtnLabel: Label = null;
    /** 提现金额 */
    wdCashAmount: number = 0;
    /** 提现档位 */
    withdrawLevel: number = 0;
    @property(Label)
    cashTotalLabel: Label = null;
    @property(RichText)
    condRichText: RichText = null;
    @property(ProgressBar)
    progressbar: ProgressBar = null;
    @property(Label)
    progressLabel: Label = null;
    isCanWD: boolean = false;
    @property(Label)
    condText1: Label = null;
    @property(RichText)
    condText2: RichText = null;
    @property(Label)
    levelWdLabel: Label = null;
    toast_game_tip_text: string = '';
    toast_game_tip_text2: string = '';
    public pageConfig: { cashTotal: number; loginDays: number; level: number; levelWithdrawConfig: { cashAmount: number; tarLevel: number; tarUserLevel: number; }; cashWithdrawConfig: Array<{ cashAmount: number; tarActiveDays: number; tarUserLevel: number; wdProgress: number; withdrawLevel: number; }>; } = null;

    chooseIdex: number = 0;

    public onShow(config: any) {


    }

    protected onEnable(): void {
        console.log(this.pageConfig);
        this.renderLocalUI();
        this.selecthitBtns[this.chooseIdex].children[0].active = true;
        this.seletionBtn(this.chooseIdex);
        // 激活选中的按钮，并取消其他按钮的激活状态
        for (let i = 0; i < this.selecthitBtns.length; i++) {
            this.selecthitBtns[i].children[0].active = (i === this.chooseIdex);
        }
    }

    protected initAfterOnLoad() {
        this.ui_container = this.node.getChildByName('container').getComponent(UITransform);
    }

    protected _click_event_: { [name: string]: (this: WithdrawPage, button: Button) => void; } = {
        close(btn) {
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            btn.interactable = false;
            BasePopUp.jelly_leave(this.node).then(() => {
                btn.interactable = true;
            });
        },
        wdBtn() {
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            if (this.isCanWD) {
                if (UserData.data.isCommer) {
                    Guide.ins.completeCurGuide(7);
                }
            } else {
                UIMgr.ins.createOneTipToast(this.toast_game_tip_text);
            }
        },
        levelWdBtn(btn) {
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            if (this.isCanWD) {

            } else {
                UIMgr.ins.createOneTipToast(this.toast_game_tip_text2);
            }
        },
        noselecthit1() {
            this.noselecthit(0);
        },
        noselecthit2() {
            this.noselecthit(1);
        },
        noselecthit3() {
            this.noselecthit(2);
        },
        noselecthit4() {
            this.noselecthit(3);
        },
        noselecthit5() {
            this.noselecthit(4);
        },
        noselecthit6() {
            this.noselecthit(5);
        }
    }

    noselecthit(index: number) {
        AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
        this.chooseIdex = index;
        // 检查当前按钮是否已被选中
        if (this.selecthitBtns[index].children[0].active) {
            // this.selecthitBtns[index].children[0].active = false;
            // this.wdCashAmount = 0;
        } else {
            // 激活选中的按钮，并取消其他按钮的激活状态
            for (let i = 0; i < this.selecthitBtns.length; i++) {
                this.selecthitBtns[i].children[0].active = (i === index);
            }
        }
        this.seletionBtn(index);
    }

    /**
     * 选择金额
     * @param btn_num 第几个按钮 0 ，1 ，2
     */
    seletionBtn(btn_num) {
        let targetCash = this.pageConfig.cashWithdrawConfig[btn_num].cashAmount;
        let tarActiveDays = this.pageConfig.cashWithdrawConfig[btn_num].tarActiveDays;
        let tarUserLevel = this.pageConfig.cashWithdrawConfig[btn_num].tarUserLevel;
        let wdProgress = this.pageConfig.cashWithdrawConfig[btn_num].wdProgress;
        let cashTotal = this.pageConfig.cashTotal;
        let loginDays = this.pageConfig.loginDays;
        let level = this.pageConfig.level;

        this.wdCashAmount = targetCash;
        // this.wdBtnLabel.string = "提现" + MainGameLogic.ins.formatCashValue(this.wdCashAmount);
        let localizedLabel = this.wdBtnLabel.node.getComponent(LocalizedLabel);
        localizedLabel.setTextKeyAndOption("pop_withdrawpage_btn_text", UIMgr.ins.formatCashValue(this.wdCashAmount));

        if (cashTotal < targetCash) {

            let localizedLabel = this.condRichText.node.getComponent(LocalizedLabel);
            localizedLabel.setTextKeyAndOption("pop_withdrawpage_cond1_text", UIMgr.ins.formatCashValue(targetCash), UIMgr.ins.formatCashValue(targetCash - cashTotal));

            this.progressbar.progress = wdProgress;
            this.progressLabel.string = Math.floor(wdProgress * 100) + `%`;
            this.isCanWD = false;
            this.toast_game_tip_text = "game_tip_text2";
        } else if (loginDays < tarActiveDays) {

            let localizedLabel = this.condRichText.node.getComponent(LocalizedLabel);
            localizedLabel.setTextKeyAndOption("pop_withdrawpage_cond2_text", tarActiveDays, 20, loginDays);

            this.progressbar.progress = wdProgress;
            this.progressLabel.string = Math.floor(wdProgress * 100) + `%`;
            this.isCanWD = false;
            this.toast_game_tip_text = "game_tip_text3";
        } else if (level < tarUserLevel) {

            let localizedLabel = this.condRichText.node.getComponent(LocalizedLabel);
            localizedLabel.setTextKeyAndOption("pop_withdrawpage_cond3_text", tarUserLevel, this.pageConfig.level);

            this.progressbar.progress = wdProgress;
            this.progressLabel.string = Math.floor(wdProgress * 100) + `%`;
            this.isCanWD = false;
            this.toast_game_tip_text = "game_tip_text4";
        }

        let localizedLabel1 = this.condText1.node.getComponent(LocalizedLabel);
        localizedLabel1.setTextKeyAndOption("pop_withdrawpage_title4_text", UIMgr.ins.formatCashValue(targetCash));

    }

    renderLocalUI() {
        this.cashTotalLabel.string = UIMgr.ins.formatCashValue(UserData.data.curCashTotal);

        let cashWithdrawConfig = this.pageConfig.cashWithdrawConfig;
        let levelWithdrawConfig = this.pageConfig.levelWithdrawConfig;

        this.condRichText.string = '';

        for (let i = 0; i < this.selecthitBtns.length; i++) {
            const element = this.selecthitBtns[i];
            element.active = false;
        }

        for (let i = 0; i < cashWithdrawConfig.length; i++) {

            this.selecthitBtns[i].active = true;
            this.selecthitBtns[i].getChildByName('cashLabel').getComponent(Label).string = UIMgr.ins.formatCashValue(cashWithdrawConfig[i].cashAmount);

        }
        let userlevel = this.pageConfig.level;
        let cashAmount = levelWithdrawConfig.cashAmount;
        let tarLevel = levelWithdrawConfig.tarLevel;
        let tarUserLevel = levelWithdrawConfig.tarUserLevel;

        this.levelWdLabel.string = UIMgr.ins.formatCashValue(cashAmount)

        if (UserData.data.curLevel < tarLevel) {
            let localizedLabel1 = this.condText2.node.getComponent(LocalizedLabel);
            localizedLabel1.setTextKeyAndOption("pop_withdrawpage_cond4_text", tarLevel, UIMgr.ins.formatCashValue(levelWithdrawConfig.cashAmount), tarLevel - UserData.data.curLevel);
            this.toast_game_tip_text2 = "game_tip_text6";
        } else {
            let localizedLabel1 = this.condText2.node.getComponent(LocalizedLabel);
            localizedLabel1.setTextKeyAndOption("pop_withdrawpage_cond5_text", tarUserLevel, tarUserLevel - userlevel);
            this.toast_game_tip_text2 = "game_tip_text7";
        }

    }

    /**
    * 格式化浮点数，保留两位小数
    * 如果小数点后的值不足0.01，则返回0
    * @param value - 需要格式化的浮点数
    * @returns 格式化后的浮点数
    */
    public formatFloatValue(value: number): number {
        // 将浮点数保留两位小数
        let formattedValue = Math.floor(value * 100) / 100;

        // 如果值小于0.01，则返回0
        if (formattedValue < 0.01) {
            return 0;
        }

        // 如果格式化后的值为0，返回0.00
        return formattedValue;
    }
}
